/**
 * 
 */
package org.demetra.jets.bdb;

import com.ericsson.otp.erlang.OtpErlangDecodeException;
import com.ericsson.otp.erlang.OtpErlangObject;
import com.ericsson.otp.erlang.OtpInputStream;
import com.ericsson.otp.erlang.OtpOutputStream;
import com.sleepycat.bind.ByteArrayBinding;
import com.sleepycat.db.DatabaseEntry;

public final class OtpObjectBinding extends ByteArrayBinding
{
	@Override
	public Object entryToObject(DatabaseEntry arg0) {
		byte[] buff = (byte[])super.entryToObject(arg0);
		OtpInputStream erlIn = new OtpInputStream(buff);
		try {
			return erlIn.read_any();
		} catch (OtpErlangDecodeException e) {
			throw new RuntimeException(e);
		}
	}
	@Override
	public void objectToEntry(Object object, DatabaseEntry entry) {
		OtpOutputStream erlOut = new OtpOutputStream((OtpErlangObject)object);
		byte[] buff=erlOut.toByteArray();
		super.objectToEntry(buff, entry);
	}
	
}